package com.test.kf21fm.controller;


import com.test.kf21fm.model.Employee;
import com.test.kf21fm.model.EmployeeVo;
import com.test.kf21fm.model.Post;
import com.test.kf21fm.service.IEmployeeService;
import com.test.kf21fm.service.IPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsy
 * @since 2025-11-06
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IPostService postService;
    @RequestMapping("/list")
    public String empList(EmployeeVo vo, Model model){
        List<EmployeeVo> employeeVos = employeeService.queryAll(vo);
        model.addAttribute("employeeVos", employeeVos);
        return "index";
    }

    @RequestMapping("/toAdd")
    public String addEmp(Model model){
        List<Post> posts = postService.list();
        model.addAttribute("postList", posts);
        return "empAdd";
    }
    @RequestMapping("/save")
    public String saveEmp(EmployeeVo vo){
        Employee e=new Employee();
        e.setPostId(Integer.parseInt(vo.getPostId().replace(",","")));
        e.setEmpName(vo.getEmpName());
        e.setEmpSex(vo.getEmpSex());
        e.setEmpAge(vo.getEmpAge());
        e.setEmpYear(vo.getEmpYear());

        boolean save = employeeService.save(e);
        return "redirect:list";
    }


}
